﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YWTSSys.Library
{
    public enum QueryOperatorsEnum
    {
        /// <summary>
        /// like '%aa%'
        /// </summary>
        包含,
        /// <summary>
        /// not like '%aa%'
        /// </summary>
        不包含,
        /// <summary>
        /// like 'aa%'
        /// </summary>
        开始于,
        /// <summary>
        /// like '%aa'
        /// </summary>
        结束于,
        /// <summary>
        /// ='aa'
        /// </summary>
        等于,
        /// <summary>
        /// &lt;&gt; 'aa'
        /// </summary>
        不等于,
        /// <summary>
        /// &gt; 'aa'
        /// </summary>
        大于,
        /// <summary>
        ///  &lt;'aa'
        /// </summary>
        小于,
        /// <summary>
        /// &gt; ='aa'
        /// </summary>
        大于等于,
        /// <summary>
        ///  &lt;='aa'
        /// </summary>
        小于等于,
        /// <summary>
        /// is null
        /// </summary>
        等于空,
        /// <summary>
        /// is not null
        /// </summary>
        不等于空,
        /// <summary>
        /// Between and
        /// </summary>
        范围
    }
    /// <summary>
    /// 查询条件支持的运算符及表达式工具类
    /// </summary>
    public static class QueryOperatorsEnumHelper
    {
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <param name="CommonFiled"></param>
        /// <param name="CommonOperator"></param>
        /// <param name="CommonCondition">如果是[CommonOperator=范围]则此参数需用|分隔符分隔参数</param>
        /// <returns></returns>
        public static string ConvertCommonOperator(string CommonFiled, QueryOperatorsEnum CommonOperator, string CommonCondition)
        {
            string _selectFiled = CommonFiled;
            QueryOperatorsEnum _selectOperator = CommonOperator;
            string _inputWhereStr = CommonCondition.Trim().Replace("'", "''");

            if (string.IsNullOrEmpty(_selectFiled))
            {
                return "";
            }
            string _FiledSqlWhere = "{0} {1} {2}";

            if (_selectOperator != QueryOperatorsEnum.不等于空 && _selectOperator != QueryOperatorsEnum.等于空)
            {
                if (string.IsNullOrEmpty(_inputWhereStr))
                {
                    return "";
                }
            }
            switch (_selectOperator)
            {
                case QueryOperatorsEnum.包含:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "like", "'%" + _inputWhereStr + "%'");
                    break;
                case QueryOperatorsEnum.不包含:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "not like", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.不等于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "<>", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.不等于空:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "is not null", "");
                    break;
                case QueryOperatorsEnum.大于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, ">", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.大于等于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, ">=", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.等于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "=", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.等于空:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "is null", "");
                    break;
                case QueryOperatorsEnum.结束于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "like ", "'%" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.开始于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "like ", "'" + _inputWhereStr + "%'");
                    break;
                case QueryOperatorsEnum.小于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "< ", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.小于等于:
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, "<= ", "'" + _inputWhereStr + "'");
                    break;
                case QueryOperatorsEnum.范围:
                    string __inputWhereStr_from = _inputWhereStr.Split('|')[0];
                    string __inputWhereStr_to = _inputWhereStr.Split('|')[1];
                    if (string.IsNullOrEmpty(__inputWhereStr_to) || string.IsNullOrEmpty(__inputWhereStr_to))
                    {
                        _FiledSqlWhere = "";
                        break;
                    }
                    _FiledSqlWhere = string.Format(_FiledSqlWhere, _selectFiled, " between ", "'" + __inputWhereStr_from + "' and '" + __inputWhereStr_to + "'");
                    break;
                default:
                    _FiledSqlWhere = "";
                    break;
            }
            return _FiledSqlWhere;
        }
    }
}
